import http from '../api/httpRequest.js'
import {TERMINAL_TYPE} from "../api/enums.js"

export default {

	state: {
		friends: [],
		activeFriend: null,
		timer: null
	},
	mutations: {
		setFriends(state, friends) {
			state.friends = friends;
		},
		updateFriend(state,friend){
			state.friends.forEach((f,index)=>{
				if(f.id==friend.id){
					// 拷贝属性
					let online = state.friends[index].online;
					Object.assign(state.friends[index], friend);
					state.friends[index].online =online;
				}
			})
		},
		activeFriend(state, idx) {
			state.activeFriend = state.friends[idx];
		},
		removeFriend(state, idx) {
			if (state.friends[idx] == state.activeFriend) {
				state.activeFriend = null;
			}
			state.friends.splice(idx, 1);
		},
		addFriend(state, friend) {
			state.friends.push(friend);
		},
		refreshOnlineStatus(state){
			let userIds = [];
			let userItems = [];
			let arr = [];
			let myCollection = [];
			let newData = [];


			if(state.friends.length ==0){
				return;
			}

			function factorial(friends, userItems, userIds) {
				//迭代
				arr.forEach(n => {
					//forEach循环
					let nstr = JSON.stringify(n)
					console.log("==="+nstr);
					if (nstr.includes('"flag":"1"') && nstr.includes('children')  ) {
						let newStr = n.children;
						return factorial(newStr)
					}else{
						myCollection.push(n)
						newData.push(n.id)
						console.log("===>>"+n.id);
					}
				})
			}

			factorial(state.friends,userItems,userIds)
			//state.friends.forEach((f)=>{userIds.push(f.id)});
			http({
				url: '/user/terminal/online',
				method: 'get',
				params: {userIds: userIds.join(',')}
			}).then((onlineTerminals) => {
				this.commit("setOnlineStatus",onlineTerminals);
			})

			// 30s后重新拉取
			clearTimeout(state.timer);
			/*state.timer = setTimeout(()=>{
				this.commit("refreshOnlineStatus");
			},30000)*/
		},
		setOnlineStatus(state,onlineTerminals){
			state.friends.forEach((f)=>{
				let userTerminal = onlineTerminals.find((o)=> f.id==o.userId);
				if(userTerminal){
					f.online = true;
					f.onlineTerminals = userTerminal.terminals;
					f.onlineWeb = userTerminal.terminals.indexOf(TERMINAL_TYPE.WEB)>=0
					f.onlineApp = userTerminal.terminals.indexOf(TERMINAL_TYPE.APP)>=0
				}else{
					f.online = false;
					f.onlineTerminals = [];
					f.onlineWeb = false;
					f.onlineApp = false;
				}
			});
			// 在线的在前面
			state.friends.sort((f1,f2)=>{
				if(f1.online&&!f2.online){
					return -1;
				}
				if(f2.online&&!f1.online){
					return 1;
				}
				return 0;
			});
		},
		clear(state) {
			clearTimeout(state.timer);
			state.friends = [];
			state.timer = null;
			state.activeFriend = [];
		}
	},
	actions: {
		loadFriend(context) {
			return new Promise((resolve, reject) => {
				http({
					url: '/friend/list',
					method: 'GET'
				}).then((friends) => {
					//let friendsjson0 = '[{"id": 0, "nickName": "眉山机构","flag":"0","children": [{"id": 0,"nickName": "一车间","flag":"0","children": [{"id":0,"flag":"1","nickName":"mb2","headImage":""},{"id":1,"flag":"1","nickName":"mb3","headImage":""}]}]}]'
					//let firendsjson1 = '[{"id":6,"nickName":"mb2","headImage":""}]'
					//let friendsjson2 = '[{"id": 0,"pid": 0, "nickName": "眉山机构","flag":"0","children": [{"id": 0,"nickName": "一车间","flag":"0","children": [{"id":0,"flag":"1","nickName":"mb2","headImage":""},{"id":1,"flag":"1","nickName":"斗庆丰","headImage":""}]},{"id": 0,"nickName": "二车间","flag":"0","children": [{"id":0,"flag":"1","nickName":"mb4","headImage":""},{"id":1,"flag":"1","nickName":"mb5","headImage":""}]}]}]'
					//let friends11 = JSON.parse(friends)
					let f1 = JSON.stringify(friends)
					console.log("网络结果："+f1)
					/*console.log("实际结果："+friendsjson2)
					friends = JSON.parse(friendsjson2)*/
					context.commit("setFriends", friends);
					//context.commit("refreshOnlineStatus");
					resolve()
				}).catch((res) => {
					reject();
				})
			});
		}
	}
}
